<template>
  <div class="app-container">
    <el-row class="row bg-white">
      <div class="action">
        <div class="chart-filter">
          <el-select clearable  size="small" style="width: 230px;" @change="handlePlanChange" v-model="listQuery.adversting_id" filterable placeholder="选择推广计划">
            <el-option v-for="item in  planOptions" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
          <el-date-picker
            v-model="pick_date_time"
            :editable="false"
            size="small"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
            @change="handleDatePickChange">
          </el-date-picker>
          <!--<el-button class="green-btn el-f-r" size="small" type="primary" >下载报表<i class="el-icon-download el-icon&#45;&#45;right"></i></el-button>-->
        </div>
      </div>
      <label></label>
      <el-table :data="list"  element-loading-text="Loading" border fit highlight-current-row>
        <el-table-column align="center" label='计划名称' >
          <template slot-scope="scope">
            {{scope.row.name}}
          </template>
        </el-table-column>
        <el-table-column label="曝光数">
          <template slot-scope="scope">
            {{scope.row.pv}}
          </template>
        </el-table-column>
        <el-table-column label="点击量">
          <template slot-scope="scope">
            {{scope.row.click_count}}
          </template>
        </el-table-column>
        <el-table-column label="点击均价">
          <template slot-scope="scope">
            {{scope.row.price}}
          </template>
        </el-table-column>
        <el-table-column label="花费（元/天）">
          <template slot-scope="scope">
            {{scope.row.daily_price}}
          </template>
        </el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">
            {{scope.row.created_at}}
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            {{scope.row.status_str}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="230" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <router-link class="link-type" v-if="btnPlanPreview" :to="'/plan/view/'+scope.row.id">
              <el-button  size="mini"><svg-icon icon-class="preview" style="margin-right: 5px" />预览</el-button>
            </router-link>
            <router-link class="link-type"  v-if="(scope.row.status==2 || scope.row.status==4)&& btnPlanEdit"  :to="'/plan/edit/'+scope.row.id">
              <el-button size="mini"><svg-icon icon-class="edit" style="margin-right: 5px" />编辑</el-button>
            </router-link>
            <router-link class="link-type" v-if="(scope.row.status==2) && btnPlanReview"  :to="'/plan/review/'+scope.row.id">
              <el-button size="mini"><svg-icon icon-class="edit" style="margin-right: 5px" />审核</el-button>
            </router-link>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-row>
  </div>
</template>

<script>
import { getList, getPagingList } from '@/api/advertsing'
import { parseTime, in_array } from '@/utils/index'

export default {
  name: 'plan',
  data() {
    return {
      list: [],
      total: null,
      listLoading: true,
      group: '',
      listQuery: {
        page: 1,
        pageSize: 10,
        start: '',
        end: '',
        adversting_id: ''
      },
      planOptions: [],
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      pick_date_time: '',
      btnPlanPreview: false,
      btnPlanEdit: false,
      btnPlanReview: false
    }
  },
  created() {
    this.getButtonStatus()
    this.getPlanOption()
    this.getList()
  },
  methods: {
    getPlanOption() {
      getList().then(response => {
        if (response.code === 1) {
          this.planOptions = response.data
        }
      })
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handlePlanChange() {
      this.getList()
    },
    handleDatePickChange() {
      if (this.pick_date_time) {
        this.listQuery.start = parseTime(new Date(this.pick_date_time[0]), '{y}-{m}-{d}')
        this.listQuery.end = parseTime(new Date(this.pick_date_time[1]), '{y}-{m}-{d}')
      } else {
        this.listQuery.start = ''
        this.listQuery.end = ''
      }
      this.getList()
    },
    getList() {
      getPagingList(this.listQuery).then(response => {
        if (response.code === 1) {
          this.list = response.data.list
          this.total = response.data.total
          setTimeout(() => {
            this.listLoading = false
          }, 1.5 * 1000)
        }
      })
    },
    getButtonStatus() {
      const btnList = this.$store.getters.buttons
      console.log(btnList)
      if (in_array('planPreview', btnList)) {
        this.btnPlanPreview = true
      }
      if (in_array('planEdit', btnList)) {
        this.btnPlanEdit = true
      }
      if (in_array('planReview', btnList)) {
        this.btnPlanReview = true
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
